package com.hospital.datasource.config;

import com.alibaba.druid.util.StringUtils;
import com.alibaba.ttl.TransmittableThreadLocal;

import java.util.ArrayDeque;
import java.util.Deque;

public class ThreadContextHolder {

    private static final ThreadLocal<Deque<String>> CONTENT_HOLDER=new TransmittableThreadLocal<Deque<String>>(){
        @Override
        protected Deque<String> initialValue() {
            return new ArrayDeque<>();
        }
    };

    private ThreadContextHolder(){}

    public static String peek(){
        return CONTENT_HOLDER.get().peek();
    }

    public static String element(){
        Deque<String> strings=CONTENT_HOLDER.get();
        if(strings==null||strings.size()==0){
            return null;
        }
        return strings.element();
    }

    public static void push(String ds){
        CONTENT_HOLDER.get().push(StringUtils.isEmpty(ds)?"":ds);
    }

    public static void poll(){
        Deque<String> deque=CONTENT_HOLDER.get();
        deque.poll();
        if(deque.isEmpty()){
            CONTENT_HOLDER.remove();
        }
    }

    public static void clear(){
        CONTENT_HOLDER.remove();
    }
}
